//app.js
var t = require('./utils/question.js');

App({
  onLaunch: function () {
    var category = wx.getStorageSync('category');
    if(category){
     // t.initAllQuestionFromStorage('q_'+category.id,'qid_'+category.id)
     t.initAllQuestionFromStorage('q'+category.id,'qid'+category.id)
    }
  },
  globalData: {
    userInfo: null,
    url: "https://m.sxylrongchuang.com/exam/",
    uid: '',
    openid: '',
    spid: 0,
    permission:false,
    urlImages: '',
  },
  interfaceAddress: {
     
      categoryList: "/api/category/list",
      questionCount: "/api/question/questionCount",
      questionList: "/api/question/questionList",
      getSettingValue: "/api/question/getSettingValue",
      getPermission: "/api/userCategory/checkPermission",
      activate: "/api/userCategory/activate",
      getCategoryDetail: "/api/category/get_category_detail",
      getWxUserInfo:"/api/auth/appletsLogin",
      getCurrentUserInfo:"/api/auth/getCurrentUserInfo",
      saveAnswerResult:"/api/answerRecord/save_result",
      saveExamStorage:"/api/storage/add",
      getMoniHistory:"/api/answerRecord/get_moni_history",
      getRankList:"/api/answerRecord/get_rank_list",
      syncDataFromDB:"/api/storage/list",
      perfectUserInfo:"/api/auth/perfectUserInfo",
      binding:"/api/auth/binding",
      paperList:"/api/paper/paperList",
  },
  info: !1,
  setIdsStroage: function (e, t, n, i) {
    
    wx.getStorage({
      key: e + "" + t,
      success: function (o) {
        for (var s = o.data, r = [], c = 0; c < s.length; c++) r.push(Object.keys(s[c]).toString());
        if (r.indexOf(n.toString()) > -1)
          for (c = 0; c < s.length; c++) Object.keys(s[c]).indexOf(n) > -1 && -1 == s[c][n].indexOf(i) && s[c][n].push(i);
        else {
          var a = {};
          a[n] = [], a[n].push(i), s.push(a);
        }
        wx.setStorage({
          key: e + "" + t,
          data: s
        });
        console.log("sdddd="+JSON.stringify(s))
        getApp().saveStorage(e,JSON.stringify(s),t); 
      }, fail: function () {
        var o = [], s = {};
        s[n] = [], s[n].push(i), o.push(s), wx.setStorage({
          key: e + "" + t,
          data: o
        });
        
      }
    })
  },
  removeids: function(e,c,n) {
  
    wx.getStorage({
      key: e + "" + c,
      success: function (t) {
        for (var i = t.data, o = 0; o < i.length; o++) if (i[o][Object.keys(i[o]).toString()].indexOf(n) > -1) {
          var s = i[o][Object.keys(i[o]).toString()].indexOf(n);
          i[o][Object.keys(i[o]).toString()].splice(s, 1), 0 == i[o][Object.keys(i[o]).toString()].length && i.splice(o, 1);
        }
        wx.setStorage({
          key: e + "" + c,
          data: i
        });
        getApp().saveStorage(e,i,c) 
      }
    });
   
  },
  saveInfo: function(e,t,n){
    wx.getStorage({
      key: e + "" + t,
      success: function(e){
        var t = e.data;
        getApp().info = !1;
        for (var i = 0; i < t.length; i++) if (t[i][Object.keys(t[i]).toString()].indexOf(n) > -1) return console.log(t[i][Object.keys(t[i]).toString()].indexOf(n) > -1),
          void (getApp().info = !0);
      }
    });
  },
  saveStorage: function(s_key,s_value,category) {
    var uid = wx.getStorageSync('uid');
    wx.request({
      url: getApp().globalData.url + getApp().interfaceAddress.saveExamStorage,
      method: 'post',
      dataType: 'json',
      data: { 'userId': uid, 'categoryId': category, 'storageKey': s_key,'storageValue':s_value},
      success: function (res) {
        console.log(res)
      }
    })
  },

  SyncDataFromDB: function(uid,categoryId) {
    wx.request({
      url: getApp().globalData.url + getApp().interfaceAddress.syncDataFromDB +'?userId='+uid+'&categoryId='+categoryId,
      method: 'get',
      dataType: 'json',
      data: { 
        //'userId': uid, 
        //'categoryId': categoryId
      },
      success: function (res) {
        var dbData = res.data.data;
        console.log(dbData.length);
        for(var i=0;i<dbData.length;i++){
          var dbValue = JSON.parse(dbData[i].storageValue);
          console.log(typeof(dbValue));
           wx.setStorageSync(dbData[i].storageKey+dbData[i].categoryId,dbValue);
        }

      }
    })
  },

  getPermission:function(uid,catId){
    var that = this
    wx.request({
      url: getApp().globalData.url + getApp().interfaceAddress.getPermission,
      method: 'get',
      dataType: 'json',
      data: {
        uid: uid,
        categoryId: catId
      },
      success: function (res) {
        if (res.data.data.value == "true"){
          getApp().globalData.permission = true ;
        }else{
          getApp().globalData.permission = false ;
        }
      }
    })
    
  },

})